package com.health.viewscontrols.focusgroup;

import java.util.ArrayList;

import com.health.viewscontrols.NovaFragmentActivity;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.GXB_SfFragment;
import com.health.tools.GlobalConts;
import com.novabluetooth.views.ExamHistoryActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 重点人群--冠心病
 * @author nova
 *
 */
public class GXBActivity extends NovaFragmentActivity implements
		OnClickListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private TextView title;
	private Button btn_back;
	public static Button btn_up;
	FragmentAdapter adapter;
	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.gxy_main);
		initView();
		addListener();
	}

	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
	private void initView() {
		title=(TextView)findViewById(R.id.title);
		
		btn_back=(Button)findViewById(R.id.btn_back);
		btn_up=(Button)findViewById(R.id.btn_up);
		
		
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton maternity_base = (RadioButton) findViewById(R.id.sf_info_base);
		
		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(maternity_base);

		maternity_base.setOnClickListener(this);

		Fragment baseFragement = new GXB_SfFragment();
		fragments = new ArrayList<Fragment>();
		fragments.add(baseFragement);

		adapter = new FragmentAdapter(
				getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		title.setText("冠心病  —— 随访信息");
		setChecked(0);
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {

			}

			@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		
		btn_back.setOnClickListener(this);
		btn_up.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.sf_info_base) {
			viewPager.setCurrentItem(0);
		} else if (id == R.id.btn_back) {
			this.finish();
		} else if (id == R.id.btn_up) {
			//保存
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			currentFragment.save();
		} else {
		}
	
		
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		onFragmentSave fragment = (onFragmentSave) adapter.getCurrentFragment();
		if (requestCode == GlobalConts.RequestXueya && resultCode == ExamHistoryActivity.ResultXueya) {
			fragment.setXueya(data.getStringExtra("高压"), data.getStringExtra("低压"), data.getStringExtra("脉率"));
		} else if (requestCode == GlobalConts.RequestTizhong && resultCode == ExamHistoryActivity.ResultTizhong) {
			fragment.setTizhong(data.getStringExtra("value"));
		} 
	}
	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {
		for (RadioButton rbtn : radioButtons) {
			rbtn.setBackground(null);
		}
		radioButtons.get(pos).setBackgroundResource(R.drawable.rd_chek_center);
		if(pos==0){
			title.setText("冠心病  —— 随访信息");
		}
	}
}
